package cn.wit.summit.chwan.ui.receiver;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;

import cn.wit.summit.chwan.utils.StringUtils;

/**
 * Created by pengsk on 2018/4/9.
 */

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
            String action=intent.getAction();
            if (action.equals(Intent.ACTION_PACKAGE_ADDED)){
                instalApk(context,intent);
            }else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)){
                uninstalApk(context,intent);
            }else if (action.equals(Intent.ACTION_PACKAGE_REPLACED)){
                replaceApk(context,intent);
            }else if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){
                receivedDownload(context,intent);
            }
    }


    /**
     * 安装应用程序
     */
    void instalApk(Context context, Intent intent) {
//        Log.v("infoo","时间 add     "+System.currentTimeMillis()+"");
        String packageName = intent.getData().getSchemeSpecificPart();
        if (StringUtils.isNotEmpty(packageName)) {
            Intent intent2 = new Intent();
            intent2.setAction(BroadcastAction.ACTION_INSTALL_BROADCAST_RESVERED);
            //要发送的内容
            intent2.putExtra("packageName", packageName);
            intent2.putExtra("actionFrom", Intent.ACTION_PACKAGE_ADDED);
            //发送 一个无序广播
            context.sendBroadcast(intent2);
//            APPInstakService_.intent(context).extra("packageName",packageName).extra("actionFrom",Intent.ACTION_PACKAGE_ADDED).start();
        }
    }

    /**
     * 卸载应用程序
     */
    void uninstalApk(Context context, Intent intent) {
//        Log.v("infoo","时间  remove   "+System.currentTimeMillis()+"");
        String packageName = intent.getData().getSchemeSpecificPart();
        if (StringUtils.isNotEmpty(packageName)) {
            Intent intent2 = new Intent();
            intent2.setAction(BroadcastAction.ACTION_INSTALL_BROADCAST_RESVERED);
            //要发送的内容
            intent2.putExtra("packageName", packageName);
            intent2.putExtra("actionFrom", Intent.ACTION_PACKAGE_REMOVED);
            //发送 一个无序广播
            context.sendBroadcast(intent2);
        }
    }

    /**
     * 替换应用程序
     */
    void replaceApk(Context context, Intent intent) {
//        Log.v("infoo","时间replace     "+System.currentTimeMillis()+"");
        String packageName = intent.getData().getSchemeSpecificPart();
        if (StringUtils.isNotEmpty(packageName)) {
            Intent intent2 = new Intent();
            intent2.setAction(BroadcastAction.ACTION_INSTALL_BROADCAST_RESVERED);
            //要发送的内容
            intent2.putExtra("packageName", packageName);
            intent2.putExtra("actionFrom", Intent.ACTION_PACKAGE_REPLACED);
            //发送 一个无序广播
            context.sendBroadcast(intent2);
        }
    }

    void receivedDownload(Context context, Intent intent) {
        long myDwonloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

        SharedPreferences sPreferences = context.getSharedPreferences("downloadplato", 0);

        long refernece = sPreferences.getLong("plato", 0);

        if (refernece == myDwonloadID) {

            String serviceString = Context.DOWNLOAD_SERVICE;

            DownloadManager dManager = (DownloadManager) context.getSystemService(serviceString);

            Intent install = new Intent(Intent.ACTION_VIEW);

            Uri downloadFileUri = dManager.getUriForDownloadedFile(myDwonloadID);

            install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");

            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(install);
        }


    }
}
